import requests
import pycurl
import StringIO

class GitTesting():


    def __init__(self):
        self.is_ip=False
        self.c= pycurl.Curl()
        self.s = StringIO.StringIO()
        self.c.setopt(pycurl.USERAGENT,'Miozilla/4.0 (compatible; MSIE 6.0; WindowsNT 5.1');
        self.c.setopt(pycurl.MAXREDIRS, 1)
        self.c.setopt(pycurl.SSL_VERIFYPEER, 0)   
        self.c.setopt(pycurl.SSL_VERIFYHOST, 0)
        self.c.setopt(pycurl.TIMEOUT, 2)#down time
        self.c.setopt(pycurl.ENCODING, 'gzip,deflate')
    def _ip_check(self,ip):
        q = ip.split('.')
        return len(q) == 4 and len(filter(lambda x: x >= 0 and x <= 255, map(int, filter(lambda x: x.isdigit(), q)))) == 4

    def _is_really(self):
        if self.html[0:4]=='DIRC':
            return True
        return False

    def _playload(self):
        playload=[]
        if self.is_ip:
            for p in self.port:
                temp='http://'+self.host+':'+str(p)+'/.git/index'
                playload.append(temp)
        else :playload.append('http://'+self.www+'/.git/index')
        self.playload=playload
        

    def _hacking_git(self,playload):
        
        try:
           
            self.c.setopt(pycurl.URL, playload)
            self.c.setopt(pycurl.WRITEFUNCTION,self.s.write)
            self.c.perform()
            self.html=self.s.getvalue()
            if self.c.getinfo(pycurl.HTTP_CODE)==200 and self._is_really(): return '[+] : %s is git'%playload[7:playload.find('/',7)]
            else: return '[-] : %s is not git'%playload[7:playload.find('/',7)]
       
        except Exception,e:
           
            return '[-] : %s is time out'%playload[7:playload.find('/',7)]
    
    def run(self,host_port):
        if type(host_port)==type('str'):
            
            self.www=host_port
        else:
            self.is_ip=True
            self.host=host_port.items()[0][0]
            self.port=host_port.items()[0][1]
        self._playload()
        
        res=[self._hacking_git(i) for i in self.playload]
        return res

if __name__  == '__main__':
      
      G=GitTesting()
      print G.run({'180.153.69.79': [80, 8080]})
      #G._hacking_git("my.oschina.net/u/929415/blog/219850")